package com.hhkj.admin.question.common.utils;

import com.hhkj.admin.question.common.cont.QuestionTypeEnum;
import com.hhkj.admin.question.model.extend.QuestionImportModel;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.utils.excl.utils.ExcelUtils;
import com.ruoyi.common.utils.excl.workbook.*;
import org.apache.poi.ss.usermodel.*;

import java.util.ArrayList;

/**
 * QuestionImportTemplateUtils
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/7/14 16:11
 */
public class QuestionImportTemplateUtils
{
    /**
     * 设置表格下拉选框信息
     *
     * @param template 模板信息
     */
    public static void supplySelectList(Workbook template)
    {
        int sheetAmount = template.getNumberOfSheets();
        for (int i = 0; i < sheetAmount; i++) {
            WorkbookUtils.selectList(i, template, 2, 0, SysConst.Flag.arrayName());
            WorkbookUtils.selectList(i, template, 2, 1, QuestionTypeEnum.arrayName());
        }
    }

    /**
     * 设置表格格式
     *
     * @param template 模板信息
     */
    public static void formatHeader(Workbook template)
    {
        int sheetAmount = template.getNumberOfSheets();
        ArrayList<CellStyleStrategy> cellStyleStrategyList = new ArrayList<>();
        cellStyleStrategyList.add(DataFormatStrategy.getInstance());
        cellStyleStrategyList.add(CellCenterStrategy.getInstance());
        CellStyle cellStyle = ExcelUtils.createCellStyle(template, cellStyleStrategyList);
        for (int i = 0; i < sheetAmount; i++) {
            formatHeader(template, i);
            Sheet sheetAt = template.getSheetAt(i);
            ExcelUtils.setSheetDefaultColumnStyle(sheetAt, cellStyle, 20);
        }
    }

    /**
     * 设置表格格式
     *
     * @param template 模板信息
     * @param index    下标
     */
    public static void formatHeader(Workbook template, Integer index)
    {
        //1.表头说明
        Sheet firstSheet = template.getSheetAt(index);
        Row firstRow = RowUtils.getFirstRow(firstSheet);
        Cell titleCell = CellUtils.getFirstCell(firstRow);
        // 设置标题行高
        CellUtils.wrapText(template, titleCell);
        RowUtils.setHeight(firstRow, QuestionImportModel.TITLE_HEIGHT);
        //标题字体格式
        Font titleFont = FontUtils.createFont(template, QuestionImportModel.EXCEL_NAME_FONT_HEIGHT, true, Font.COLOR_RED);
        CellUtils.updatePartContentFont(titleCell, titleFont, QuestionImportModel.FILE_NAME);
        //必填字段标红
        SheetUtils.updateRequiredHeaderStyle(template, template.getSheetAt(index), 1, QuestionImportModel.class);

    }
}
